<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <el-button @click="handleCreate">添加菜单</el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column prop="id" label="编号"> </el-table-column>
      <el-table-column prop="name" label="菜单名称"> </el-table-column>
      <el-table-column prop="description" label="菜单级数">
         <template slot-scope="scope">
           {{ ['一级', '二级', '三级'][scope.row.level] }}
        </template>
      </el-table-column>
      <el-table-column prop="createdTime" label="前端图标"> </el-table-column>
      <el-table-column prop="orderNum" label="排序"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
           <el-button type="text" @click="handleUpdate(scope.row.id)">编辑</el-button>
           <el-button type="text" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script lang="ts">
import Vue from 'vue'
// import DialogForm from './components/dialog-form.vue'
import { getAllMenus, deleteMenu } from '@/services/menu'

export default Vue.extend({
  name: 'MenuIndex',

  components: {
    // DialogForm
  },

  data () {
    return {
      tableData: [],
      dialogVisible: false,
      roleForm: {
        id: '',
        name: '',
        code: '',
        description: ''
      }
    }
  },

  mounted () {
    this.loadMenus()
  },

  methods: {
    loadMenus () {
      getAllMenus().then(res => {
        if (res.data.code === '000000') {
          this.tableData = res.data.data
        }
      })
    },

    handleCreate () {
      this.$router.push({ name: 'menu-create' })
    },

    handleUpdate (id: number) {
      this.$router.push({
        name: 'menu-update',
        query: { id: id + '' }
      })
    },

    handleDelete (id: number) {
      this.$confirm('确定删除?', '提示').then(() => {
        deleteMenu(id).then(res => {
          if (res.data.code === '000000') {
            this.$message.success('删除成功')
            this.loadMenus()
          }
        })
      }).catch(() => {
        this.$message('已取消删除')
      })
    }
  }
})
</script>

<style lang="scss" scoped>
.input-name{
  margin: 10px;
  width: 150px;
}
</style>
